/**
 * @file engineer_mode_location.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_ENGINEER_MODE!= 0

/*********************
 *      DEFINES
 *********************/
#define ENGINEER_MODE_LOCATION_LIST_CNT    1

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static lv_engineer_location_obj_ext_t * location_get_ext(void);
static void location_btn_rel_event_cb(lv_obj_t * btn, lv_event_t e);
static void location_prepare_destory(lv_obj_t * activity_obj);

static void auto_dialer_create(lv_obj_t * par);
static void auto_dialer_btn_event_cb(lv_obj_t * obj, lv_event_t e);
static void auto_dialer_start_test(void);
static void auto_dialer_stop_test(void);
static void auto_dialer_end_timeout_cb(void * para);
static void auto_dialer_call_timeout_cb(void * para);
static void auto_dialer_prepare_destory(lv_obj_t * activity_obj);

/**********************
 *  STATIC VARIABLES
 **********************/
const lv_engineer_item_t engineer_mode_location_list[] = {
    { WATCH_TEXT_ID_EMODE_LOC_AUTO_DIALER, auto_dialer_create, auto_dialer_prepare_destory},
};

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
lv_obj_t * engineer_mode_location_create(lv_obj_t * activity_obj)
{
    lv_obj_t * obj;
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_ENGINEER_MODE_LOCATION;
        activity_ext.create = engineer_mode_location_create;
        activity_ext.prepare_destory = location_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(NULL == activity_obj) {
            return NULL;
        }
    }

    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(NULL == obj) {
        return NULL;
    }

    /*Allocate the object type specific extended data*/
    lv_engineer_location_obj_ext_t * loc_ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_engineer_location_obj_ext_t));
    LV_ASSERT_MEM(loc_ext);
    if(loc_ext == NULL) {
        return NULL;
    }

    lv_obj_t * title = lv_label_create(obj, NULL);
    lv_obj_set_size(title, LV_HOR_RES, LV_VER_RES / 4);
    lv_label_set_text_id(title, WATCH_TEXT_ID_ENGINEER_MODE_LOCATION);
    lv_obj_add_style(title, LV_LABEL_PART_MAIN, &lv_watch_font30);
    lv_obj_align(title, NULL, LV_ALIGN_IN_TOP_MID, 0, 5);

    lv_obj_t * list = lv_list_create(obj, NULL);
    lv_obj_set_size(list, LV_HOR_RES, LV_VER_RES - lv_obj_get_height(title) - 10);
    lv_page_set_edge_flash(list, true);
    lv_obj_add_style(list, LV_LIST_PART_EDGE_FLASH, &lv_watch_cont_opa1);
    lv_obj_align(list, title, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);

    lv_engineer_main_obj_ext_t * ext = engineer_mode_get_ext();
    lv_obj_add_style(list, LV_LIST_PART_BG,  &lv_watch_style_transp);

    uint8_t i = 0;
    lv_obj_t * btn;
    lv_obj_t * label;
    for(i = 0; i < ENGINEER_MODE_LOCATION_LIST_CNT; i++) {
        btn = lv_list_add_btn(list, NULL, lv_lang_get_text(engineer_mode_location_list[i].txtId));
        lv_watch_set_free_num(btn, i);
        lv_obj_add_style(btn, LV_BTN_PART_MAIN, &(ext->list_btn_st));
        lv_obj_set_style_local_border_side(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_BORDER_SIDE_NONE);
        lv_obj_set_style_local_border_side(btn, LV_BTN_PART_MAIN, LV_STATE_PRESSED, LV_BORDER_SIDE_NONE);
        lv_obj_set_style_local_border_side(btn, LV_BTN_PART_MAIN, LV_STATE_FOCUSED, LV_BORDER_SIDE_NONE);
        lv_obj_set_event_cb(btn, location_btn_rel_event_cb);

        label = lv_obj_get_child(btn, NULL);
        lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
        lv_label_set_long_mode(label, LV_LABEL_LONG_BREAK);
        lv_obj_set_width(label, LV_HOR_RES - 10);
    }

    lv_obj_set_click(list, true);

    return obj;
}

void engineer_mode_auto_dialer_test_result_ind(lv_engineer_auto_dialer_event_t evt)
{
    lv_engineer_location_obj_ext_t * loc_ext = location_get_ext();
    char status[32];

    switch(evt) {
        case EVENT_END_CALL:
            if(++loc_ext->curr_call_index < loc_ext->loops) {
                snprintf(status, 32, "(%d/%d)%s", loc_ext->curr_call_index, loc_ext->loops, (char *)lv_lang_get_text(WATCH_TEXT_ID_EVENT_END_CALL));
                lv_label_set_text(loc_ext->auto_dialer_label_status, status);

                //stop call timer
                if(NULL != loc_ext->call_timer_id) {
                    Hal_Timer_Stop(loc_ext->call_timer_id);
                    loc_ext->call_timer_id = NULL;
                }

                //start end timer
                loc_ext->end_timer_id = Hal_Timer_Start(loc_ext->end_duration * 1000, auto_dialer_end_timeout_cb, NULL, 0);
            } else {
                //stop test
                auto_dialer_stop_test();
            }
            break;

        case EVENT_FINISH:
            //stop test
            auto_dialer_stop_test();
            break;

        default:
            break;
    }
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
lv_engineer_location_obj_ext_t * location_get_ext(void)
{
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_ENGINEER_MODE_LOCATION);
    if(activity_obj == NULL) return NULL;

    lv_obj_t * watch_obj;
    lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    if(watch_obj == NULL) return NULL;

    if(watch_obj) {
        lv_engineer_location_obj_ext_t * ext = lv_obj_get_ext_attr(watch_obj);
        return ext;
    }
    return NULL;
}

static void location_btn_rel_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        uint32_t num = lv_obj_get_user_data(btn).user_num;
        lv_obj_t * obj = NULL;

        obj = engineer_mode_item_watch_obj_create(NULL, engineer_mode_location_list[num].item_prepare_destory);
        if(NULL == obj) {
            printf("%s: obj is null\n", __FUNCTION__);
        } else {
            engineer_mode_item_title_create(obj, engineer_mode_location_list[num].txtId);
            engineer_mode_location_list[num].item_content_create(obj);
        }
    }
}

static void location_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
}

static void auto_dialer_create(lv_obj_t * par)
{
    const char options_loops[] = {
        "1 loop\n"
        "5 loops\n"
        "10 loops\n"
        "15 loops\n"
        "20 loops\n"
        "25 loops\n"
        "30 loops\n"
        "35 loop\n"
        "40 loops\n"
        "45 loops\n"
        "50 loops\n"
        "55 loops\n"
        "60 loops\n"
        "65 loops\n"
        "70 loop\n"
        "75 loops\n"
        "80 loops\n"
        "85 loops\n"
        "90 loops\n"
        "95 loops\n"
        "100 loops"
    };

    const char options_duration[] = {
        "1 s\n"
        "5 s\n"
        "10 s\n"
        "15 s\n"
        "20 s\n"
        "25 s\n"
        "30 s\n"
        "35 s\n"
        "40 s\n"
        "45 s\n"
        "50 s\n"
        "55 s\n"
        "60 s"
    };

    lv_engineer_location_obj_ext_t * loc_ext = location_get_ext();
    loc_ext->auto_dialer_test_started = false;
    loc_ext->end_timer_id = NULL;
    loc_ext->call_timer_id = NULL;

    //btn
    lv_obj_t * btn = lv_btn_create(par, NULL);
    lv_obj_set_event_cb(btn, auto_dialer_btn_event_cb);
    lv_obj_set_size(btn, LV_HOR_RES * 2 / 3, 30);
    lv_obj_align(btn, NULL, LV_ALIGN_IN_TOP_LEFT, 10, 30);
    lv_obj_set_style_local_radius(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 5);

    loc_ext->auto_dialer_label_test = lv_label_create(btn, NULL);
    lv_label_set_text_id(loc_ext->auto_dialer_label_test, WATCH_TEXT_ID_START_TEST);
    lv_obj_add_style(loc_ext->auto_dialer_label_test, LV_LABEL_PART_MAIN, &lv_watch_font20);

    //label
    lv_obj_t * label_loops = lv_label_create(par, NULL);
    lv_label_set_text_id(label_loops, WATCH_TEXT_ID_LOOPS);
    lv_obj_add_style(label_loops, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_obj_align(label_loops, btn, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 10);

    lv_obj_t * label_end_duraton = lv_label_create(par, label_loops);
    lv_label_set_text_id(label_end_duraton, WATCH_TEXT_ID_END_CALL);
    lv_obj_align(label_end_duraton, label_loops, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 15);

    lv_obj_t * label_call_duraton = lv_label_create(par, label_loops);
    lv_label_set_text_id(label_call_duraton, WATCH_TEXT_ID_PHONE_CALL);
    lv_obj_align(label_call_duraton, label_end_duraton, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 15);

    lv_obj_t * label_phone = lv_label_create(par, label_loops);
    lv_label_set_text_id(label_phone, WATCH_TEXT_ID_PHONE);
    lv_obj_align(label_phone, label_call_duraton, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 5);

    lv_obj_t * label_number = lv_label_create(par, label_loops);
    lv_label_set_text(label_number, "112");
    lv_obj_align(label_number, label_phone, LV_ALIGN_OUT_RIGHT_TOP, 10, 0);

    loc_ext->auto_dialer_label_status = lv_label_create(par, label_loops);
    lv_label_set_text(loc_ext->auto_dialer_label_status, "");
    lv_obj_align(loc_ext->auto_dialer_label_status, label_phone, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 10);

    //ddlist
    loc_ext->auto_dialer_ddl_call_duraton = lv_dropdown_create(par, NULL);
    lv_dropdown_set_options(loc_ext->auto_dialer_ddl_call_duraton, options_duration);
    lv_obj_add_style(loc_ext->auto_dialer_ddl_call_duraton, LV_DROPDOWN_PART_MAIN, &lv_watch_font20);
    lv_obj_add_style(loc_ext->auto_dialer_ddl_call_duraton, LV_DROPDOWN_PART_SELECTED, &lv_watch_font20);
    lv_obj_align(loc_ext->auto_dialer_ddl_call_duraton, label_call_duraton, LV_ALIGN_OUT_RIGHT_MID, 10, 0);

    loc_ext->auto_dialer_ddl_end_duraton = lv_dropdown_create(par, loc_ext->auto_dialer_ddl_call_duraton);
    lv_dropdown_set_options(loc_ext->auto_dialer_ddl_end_duraton, options_duration);
    lv_obj_align(loc_ext->auto_dialer_ddl_end_duraton, label_end_duraton, LV_ALIGN_OUT_RIGHT_MID, 10, 0);

    loc_ext->auto_dialer_ddl_loops = lv_dropdown_create(par, loc_ext->auto_dialer_ddl_call_duraton);
    lv_dropdown_set_options(loc_ext->auto_dialer_ddl_loops, options_loops);
    lv_obj_align(loc_ext->auto_dialer_ddl_loops, label_loops, LV_ALIGN_OUT_RIGHT_MID, 10, 0);
}

static void auto_dialer_btn_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        char buf[16] = {0};
        char tmp[16] = {0};
        lv_engineer_location_obj_ext_t * loc_ext = location_get_ext();

        if(false == loc_ext->auto_dialer_test_started) {
            loc_ext->auto_dialer_test_started = true;
            loc_ext->curr_call_index = 0;
            lv_label_set_text_id(loc_ext->auto_dialer_label_test, WATCH_TEXT_ID_STOP_TEST);

            lv_dropdown_get_selected_str(loc_ext->auto_dialer_ddl_loops, buf, 16);
            sscanf(buf, "%d %s", &loc_ext->loops, tmp);
            lv_dropdown_get_selected_str(loc_ext->auto_dialer_ddl_end_duraton, buf, 16);
            sscanf(buf, "%d %s", &loc_ext->end_duration, tmp);
            lv_dropdown_get_selected_str(loc_ext->auto_dialer_ddl_call_duraton, buf, 16);
            sscanf(buf, "%d %s", &loc_ext->call_duration, tmp);

            Hal_Mem_Set(loc_ext->number, 0, NV_CONTACTS_MAX_NUMBER_LEN);
            strncpy(loc_ext->number, "112", 4);
            printf("loops=%d, end_duration=%d, call_duration=%d, number=%s\n", loc_ext->loops, loc_ext->end_duration, loc_ext->call_duration, loc_ext->number);

            //start test
            auto_dialer_start_test();
        } else {
            //stop test
            auto_dialer_stop_test();
        }
    }
}

static void auto_dialer_start_test(void)
{
    lv_engineer_location_obj_ext_t * loc_ext = location_get_ext();

    //start test
    engineer_mode_auto_dialer_test_req(true);

    //voice call
    char * number = Hal_Mem_Alloc(NV_CONTACTS_MAX_NUMBER_LEN);
    strncpy(number, loc_ext->number, NV_CONTACTS_MAX_NUMBER_LEN);
    phone_voice_call_req(number);

    //start call timer
    loc_ext->call_timer_id = Hal_Timer_Start(loc_ext->call_duration * 1000, auto_dialer_call_timeout_cb, NULL, 0);
}

static void auto_dialer_stop_test(void)
{
    lv_engineer_location_obj_ext_t * loc_ext = location_get_ext();

    loc_ext->auto_dialer_test_started = false;
    loc_ext->curr_call_index = 0;
    lv_label_set_text_id(loc_ext->auto_dialer_label_test, WATCH_TEXT_ID_START_TEST);
    lv_label_set_text_id(loc_ext->auto_dialer_label_status, WATCH_TEXT_ID_EVENT_FINISH);

    //stop timer
    if(NULL != loc_ext->end_timer_id) {
        Hal_Timer_Stop(loc_ext->end_timer_id);
        loc_ext->end_timer_id = NULL;
    }

    if(NULL != loc_ext->call_timer_id) {
        Hal_Timer_Stop(loc_ext->call_timer_id);
        loc_ext->call_timer_id = NULL;
    }

    //stop test
    engineer_mode_auto_dialer_test_req(false);
}

static void auto_dialer_end_timeout_cb(void * para)
{
    lv_engineer_location_obj_ext_t * loc_ext = location_get_ext();
    loc_ext->end_timer_id = NULL;

    auto_dialer_start_test();
}

static void auto_dialer_call_timeout_cb(void * para)
{
    lv_engineer_location_obj_ext_t * loc_ext = location_get_ext();
    loc_ext->call_timer_id = NULL;

    engineer_mode_auto_dialer_end_call_req();
}

static void auto_dialer_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();

    auto_dialer_stop_test();
}

#endif /*USE_LV_WATCH_ENGINEER_MODE*/
